package com.example.coffeeseller_2.model.model;

import android.os.AsyncTask;
import android.text.TextUtils;
import android.util.Log;

import com.example.coffeeseller_2.model.beans.CommitMaterialObject;
import com.example.coffeeseller_2.model.beans.MachineConfig;
import com.example.coffeeseller_2.model.contentprovider.Constance;
import com.example.coffeeseller_2.model.httputils.JsonUtil;
import com.example.coffeeseller_2.model.httputils.OkHttpUtil;
import com.example.coffeeseller_2.model.listenner.ICommitMaterial;
import com.example.coffeeseller_2.model.listenner.OnCommitMaterialCallBackListener;

import org.json.JSONException;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/4/27.
 */

public class CommitMaterial implements ICommitMaterial {
    private String TAG = "CommitMaterial";

    @Override
    public void Commit(List<CommitMaterialObject> list, final OnCommitMaterialCallBackListener onCommitMaterialCallBackListener) {
        final Map<String, Object> postBody = new HashMap<>();
        postBody.put("machineCode", MachineConfig.getMachineCode());
        postBody.put("bunkers", list);
//        Log.e(TAG, "commit position = 1 BunkerID is " + String.valueOf(list.get(1).getBunkerID()));
        new AsyncTask<Void, Boolean, Boolean>() {
            String response;

            @Override
            protected Boolean doInBackground(Void... params) {
                try {
                    String requestReponse = OkHttpUtil.post(Constance.COMMIT_MATERIAL_URL, JsonUtil.mapToJson(postBody));
                    if (TextUtils.equals(requestReponse, "请求地址错误")) {
                        return false;
                    }
                    if (TextUtils.equals(requestReponse, "")) {
                        return false;
                    }
                    org.json.JSONObject object = new org.json.JSONObject(requestReponse);
                    Log.e(TAG, "start commit");
                    Log.e(TAG, "result is " + object.toString());
                    if (object.getString("err").equals("")) {
                        response = "";
                        return true;
                    } else {
                        response = object.getString("err");
                    }
                } catch (IOException e) {
                    response = e.getMessage();
                } catch (JSONException e) {
                    response = e.getMessage();
                } catch (Exception e) {
                    response = e.getMessage();
                }
                return false;
            }

            @Override
            protected void onPostExecute(Boolean aBoolean) {
                if (aBoolean) {
                    onCommitMaterialCallBackListener.commitSuccess();
                } else {
                    onCommitMaterialCallBackListener.commitFailed(response);
                }
            }
        }.execute();
    }
}
